package com.xiaozhengkeji.xiaozhengblog.api;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.xiaozhengkeji.xiaozhengblog.base.entitys.ArticleClassEntity;
import com.xiaozhengkeji.xiaozhengblog.base.entitys.SystemUserEntity;
import com.xiaozhengkeji.xiaozhengblog.base.service.ArticleClassEntityService;
import com.xiaozhengkeji.xiaozhengblog.base.service.SystemUserEntityService;
import com.xiaozhengkeji.xiaozhengblog.bean.ResponseBean;
import com.xiaozhengkeji.xiaozhengblog.bean.type.ResCodeType;
import com.xiaozhengkeji.xiaozhengblog.services.bus.ArticleUtilsServiceBus;
import com.xiaozhengkeji.xiaozhengblog.utils.page.ListPager;
import com.xiaozhengkeji.xiaozhengblog.utils.page.Pagers;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * 文章分类
 */
@Slf4j
@RestController
@RequestMapping("/web/article/class")
public class ArticleClassApi {
    SystemUserEntityService systemUserEntityService;
    ArticleClassEntityService articleClassEntityService;
    ArticleUtilsServiceBus articleUtilsServiceBus;

    @Autowired
    public ArticleClassApi(SystemUserEntityService systemUserEntityService, ArticleClassEntityService articleClassEntityService, ArticleUtilsServiceBus articleUtilsServiceBus) {
        this.systemUserEntityService = systemUserEntityService;
        this.articleClassEntityService = articleClassEntityService;
        this.articleUtilsServiceBus = articleUtilsServiceBus;
    }

    /**
     * (管理后台)获取文章分类
     */
    @ApiOperation("(管理后台)获取全部分类 权限(user:author)")
    @GetMapping("get")
    @SaCheckPermission("user:author")
    public ResponseBean get(@ApiParam("分类名 空为查询全部") @RequestParam("query") String query,
                            @ApiParam("第几页") @RequestParam("pageNum") Integer pageNum,
                            @ApiParam("每页数量") @RequestParam("pageSize") Integer pageSize) {
        List<ArticleClassEntity> articleClassEntities = articleClassEntityService.queryByAll(new ArticleClassEntity());
        ListPager<ArticleClassEntity> articleClassEntityListPager = Pagers.newListPager(pageSize, articleClassEntities);
        return new ResponseBean(ResCodeType.Success, "!", articleClassEntityListPager.getPage(pageNum), articleClassEntities.size());
    }


    /**
     * (前端)获取当前可查看的全部分类(可登陆/或者游客模式)
     */
    @ApiOperation("(前端)获取当前可查看的全部分类 权限(可登陆/或者游客模式)")
    @PostMapping("getDefault")
    public ResponseBean getDefault() {
        SystemUserEntity systemUserEntity = null;
        if (StpUtil.isLogin()) {
            systemUserEntity = systemUserEntityService.selectByPrimaryKey(StpUtil.getLoginIdAsInt());
        }
        List<String> articleClassList = new LinkedList<>();
        for (ArticleClassEntity articleClass : articleClassEntityService.queryByAll(new ArticleClassEntity())) {
            String username = "tourist";
            if (systemUserEntity != null) {
                username = systemUserEntity.getUsername();
            }
            if (articleUtilsServiceBus.isUserHasArticleClassAccess(articleClass.getArticleClass(), username)) {
                articleClassList.add(articleClass.getArticleClass());
            }
        }
        return new ResponseBean(ResCodeType.Success, "获取成功!", articleClassList, articleClassList.size());
    }


    /**
     * 编辑分类
     */
    @ApiOperation("编辑一个文章分类 权限(user:author)")
    @PostMapping("edit")
    @SaCheckPermission("user:author")
    public ResponseBean edit(@ApiParam("索引") Integer id,
                             @ApiParam("分类名") String articleClass,
                             @ApiParam("分类描述") String articleDescribe,
                             @ApiParam("分类权限 (user:def)为公开") String articleClassPurview) {
        ArticleClassEntity articleClassEntity = articleClassEntityService.selectByPrimaryKey(id);
        if (articleClassEntity == null) {
            return new ResponseBean(ResCodeType.Error, "无效的索引", null, null);
        }
        articleClassEntity.setArticleClass(articleClass);
        articleClassEntity.setArticleDescribe(articleDescribe);
        articleClassEntity.setArticleClassPurview(articleClassPurview);
        if (articleClassEntityService.updateByPrimaryKey(articleClassEntity) > 0) {
            return new ResponseBean(ResCodeType.Success, "更新文章分类完成!", null, null);
        }
        return new ResponseBean(ResCodeType.Error, "数据库未响应!", null, null);
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除一个文章分类 权限(user:author)")
    @GetMapping("del")
    @SaCheckPermission("user:author")
    public ResponseBean del(@ApiParam("索引") Integer id) {
        if (articleClassEntityService.deleteByPrimaryKey(id) > 0) {
            return new ResponseBean(ResCodeType.Success, "删除文章分类成功!", null, null);
        }
        return new ResponseBean(ResCodeType.Error, "数据库未响应!", null, null);
    }

    /**
     * 创建一个文章分类
     */
    @ApiOperation("创建一个文章分类 权限(user:author)")
    @PostMapping("/create")
    public ResponseBean create(@ApiParam("分类名") String articleClass,
                               @ApiParam("分类描述") String articleDescribe,
                               @ApiParam("分类权限 (user:def)为公开") String articleClassPurview) {
        if (articleClassEntityService.queryAllByArticleClass(articleClass).size() > 0) {
            return new ResponseBean(ResCodeType.Error, "已经存在此分类", null, null);
        }
        ArticleClassEntity articleClassEntity = new ArticleClassEntity();
        articleClassEntity.setArticleClass(articleClass);
        articleClassEntity.setArticleDescribe(articleDescribe);
        articleClassEntity.setArticleClassPurview(articleClassPurview);
        if (articleClassEntityService.insert(articleClassEntity) > 0) {
            return new ResponseBean(ResCodeType.Success, "添加分类成功!", null, null);
        }
        return new ResponseBean(ResCodeType.Error, "数据库未响应!", null, null);
    }
}
